package Action;


import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.hibernate.engine.jdbc.batch.spi.BatchObserver;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

import DAO.user_bookDAO;
import Model.user;
import Model.user_book;
import Service.borrowService;
/**
 * 借书动作
 * @author MacBook
 *
 */
public class BorrowAction {
	//书本列表
	private List<user_book> list;
	
	public List<user_book> getList() {
		return list;
	}
	//借书动作
	public void borrowBook(){	
		borrowService bs = new borrowService();
		//获取请求响应
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		//获取必要信息
		int bookid = Integer.parseInt(request.getParameter("bookid"));
		String username = ((user)ActionContext.getContext().getSession().get("user")).getUsername();
		System.out.println(bookid+" "+username);
		//启动服务
		boolean isSuccess = bs.borrowBookService(username, bookid);
		try {
			if (isSuccess)
				response.getWriter().print("success");
			else
				response.getWriter().print("false");
		} catch (IOException e) {
			// TODO: handle exception
		}
	}
	//还书服务
	public void returnBook(){
		//获取请求响应
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		//获取主要信息
		int bookid = Integer.parseInt(request.getParameter("bookid"));
		int userid = ((user)ActionContext.getContext().getSession().get("user")).getUserid();
		System.out.println("userid="+userid+" bookid="+bookid);
		borrowService bs = new borrowService();
		bs.returnBookService(userid, bookid);
		
		
		try {
			response.setCharacterEncoding("utf-8");
			response.getWriter().print("success");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	//查看用户手上的书
	public String booklist(){
		int userid = ((user)ActionContext.getContext().getSession().get("user")).getUserid();
		//开启服务
		borrowService bs = new borrowService();
		list = bs.user_bookList(userid);
		return "success";
	}
	
}
